package com.tcm.ucenter.web.controller;

import com.base.core.exception.ServiceException;
import com.base.core.lang.StringUtils;
import com.base.core.utils.R;
import com.tcm.im.service.HxService;
import com.tcm.invite.service.InviteService;
import com.tcm.ucenter.service.UserDetailService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/ucenter")
public class QrcodeController {

    @DubboReference(version = "0.0.1-alpha", check = false, lazy = true)
    private UserDetailService userService;

    @DubboReference(version = "0.0.1-alpha", check = false, lazy = true)
    private HxService hxService;

    @DubboReference(version = "0.0.1-alpha", check = false, lazy = true)
    private InviteService inviteService;


    /**
     * 业务员邀请二维码
     *
     * @param cellphone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/invite/qrcode", method = RequestMethod.GET)
    public R inviteQrcode(@RequestParam("cellphone") String cellphone) throws Exception {
        try {
            if(StringUtils.isEmpty(cellphone)){
                return R.fail("缺少登录手机号");
            }
            String qrcode = inviteService.inviteQrcode(cellphone, "salesman");
            return R.success(qrcode);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * 邀请分享链接
     *
     * @param cellphone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/invite/link", method = RequestMethod.GET)
    public R inviteLink(@RequestParam("cellphone") String cellphone) throws Exception {
        try {
            if(StringUtils.isEmpty(cellphone)){
                return R.fail("缺少登录手机号");
            }
            String url = inviteService.createInviteLink(cellphone, "salesman");
            return R.success(url);
        } catch (ServiceException e) {
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }


}
